package com.jiaai.web.zfp_back.gift;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jiaai.gift.api.ILuckHisApi;
import com.jiaai.util.RestResultGenerator;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * class_name:ZBLuckHisController
 * describe:------------------>TODO
 * create_user:张昉平
 * create_date:2018/1/13
 * create_date:14:53
 */
@RestController
@RequestMapping("/api/v1/admin/luckHis")
public class ZBLuckHisController {

    @Reference
    private ILuckHisApi iLuckHisApi;



    @GetMapping("/list")
    public Object list(@RequestParam(required = false, defaultValue = "1") Integer curr, String giftName, Integer liveId){
        Map map = new HashMap();
        try {
            CountDownLatch countDownLatch = new CountDownLatch(2);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        map.put("totalNum", iLuckHisApi.getLuckPoolNo());
                    }finally {
                        countDownLatch.countDown();
                    }
                }
            }).start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        map.put("list", iLuckHisApi.getAll(curr, giftName, liveId));
                    }finally {
                        countDownLatch.countDown();
                    }
                }
            }).start();
            countDownLatch.await();
            return RestResultGenerator.genSuccessResult(map);
        }catch (Exception e){
            e.printStackTrace();
            return RestResultGenerator.genErrorResult("服务器异常");
        }
    }

    @PostMapping("/set/{num}")
    public Object set(@PathVariable String num){
        iLuckHisApi.setLuckPoolNo(num);
        return RestResultGenerator.genSuccessResult("操作成功");
    }
}
